package cn.itsource.quartz.job;

import cn.itsource.order.domain.OrderProduct;
import cn.itsource.order.service.IOrderProductService;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Component
public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IOrderProductService orderProductService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //1.获取定时任务里面携带的参数
        JobDataMap dataMap = context.getMergedJobDataMap();
        //获取orderSn
        Map<String ,Object> params = (Map<String, Object>) dataMap.get("params");

        String orderSn =params.get("orderSn").toString();

        PayBill payBill =payBillService.loadByOrderSn(orderSn);
        //查询订单
                     /*
                        此处 支付单 对应的订单有5种类型  不同的支付单对应定义有可能在 5张表里面的任意一张表
                          方案一：
                              依次去5张表里面查询  直到查出来的不为空
                          方案二：
                               在支付单里面 businessType字段，存储订单的类型
                      */
        OrderProduct orderProduct=null;
        if (payBill.getBusinessType().equals("businesstype_product")){//查询服务订单表
            orderProduct = orderProductService.loadByOrderSn(orderSn);
        }else if(payBill.getBusinessType().equals("businesstype_adopt")){//查询领养订单

        }
        //2.把对应的订单和支付单 更状态  设置更新时间    设置支付单号
        payBill.setState(-1);
        payBill.setUpdateTime(new Date());
        payBillService.saveOrUpdate(payBill);

        orderProduct.setState(-1);
        orderProductService.saveOrUpdate(orderProduct);

    }
}
